package com.goktech.controller;

import com.goktech.entity.Book;
import com.goktech.entity.type;
import com.goktech.service.BookService;
import com.goktech.service.TypeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName bookManageController
 * @Description TODO
 * @Author 邱华英
 * @Date 2022/10/7 18:45
 * @Version 1.0
 */

@Controller
@RequestMapping("/admin/book")
public class adminBookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private Book book;

    @Value("${image}")
    private String image;

    @RequestMapping("/")
    public String BookSelectAll(HttpServletRequest request){
        List<type> byAll = typeService.findByAll();
        System.out.println(byAll);
        HttpSession session = request.getSession();
        session.setAttribute("types",byAll);
        return "redirect:search/add?yeshu=0";
    }

    @RequestMapping ("/search/add")
    public String searchAdd(Integer yeshu,HttpServletRequest request){
        List<Map<String, Object>> byAll = bookService.findByAll(yeshu * 5,5);

        if(byAll.size() == 0 && yeshu!=0){
            byAll = bookService.findByAll((yeshu-1)*5,5);
        }else{
            yeshu=yeshu+1;
        }
        request.setAttribute("yeshu",yeshu);
        request.setAttribute("books",byAll);
        System.out.println("yeshu:"+yeshu);
        return "admin/bookManage";
    }

    @RequestMapping("/search/delete")
    public String searchDelete(Integer yeshu,HttpServletRequest request){
        List<Map<String, Object>> byAll = null;
        if(yeshu>1){
            yeshu=yeshu-1;
        }
        byAll = bookService.findByAll((yeshu-1)*5,5);
        request.setAttribute("books",byAll);
        request.setAttribute("yeshu",yeshu);
        return "admin/bookManage";
    }

    @PostMapping("/select")
    public String select(@Param("select") String select, @Param("text") String text, HttpServletRequest request) throws UnsupportedEncodingException {
        List<Map<String, Object>> AllList = null;
        String text1 = new String(text.getBytes("ISO-8859-1"), "utf-8").trim();
        System.out.println("text1:"+text1);
        if("bookName".equals(select)){
            book.setBName("%"+text1+"%");
            AllList  = bookService.findLikeBookName(book);
        }else if("author".equals(select)){
            book.setBAuthor("%"+text1+"%");
            AllList = bookService.findLikeAuthor(book);
        }
        System.out.println("allList:"+AllList);
        request.setAttribute("books",AllList);
        request.setAttribute("yeshu",1);
        return "admin/bookManage";
    }

    @RequestMapping ("/delete")
    public String deleteBook(@Param("bookid") int bookid){

        book.setId(bookid);
        Book byBook = bookService.findByBook(book);

        int account = byBook.getAccout();
        int yuliang = byBook.getYuliang();

        if(account == yuliang){
            bookService.deleteBook(book);
        }
        return "redirect:search/add?yeshu=0";
    }

    @PostMapping("/update")
    public String update(@RequestParam("file")MultipartFile file,@Param("book") Book book1,HttpServletRequest request){
        Book byBook = bookService.findByBook(book1);
        if(byBook != null){
            Integer accout = book1.getAccout();
            Integer accout1 = byBook.getAccout();
            Integer yuliang = 0;
            if(accout >= accout1 && accout > 0){
                yuliang = accout - accout1 + byBook.getYuliang();
            }else if(accout < accout1 && accout > 0){
                yuliang = byBook.getYuliang() - accout1 + accout;
            }
            if(yuliang >= 0){
                book1.setYuliang(yuliang);
            }else {
                return "redirect:search/add?yeshu=0";
            }

            book1.setRukushijian(byBook.getRukushijian());
            book1.setPhoto(byBook.getPhoto());
            try {
                String bName =new String(request.getParameter("bName").getBytes("ISO-8859-1"), "utf-8");
                book1.setBName(bName);
                String introduction = new String(request.getParameter("introduction").getBytes("ISO-8859-1"), "utf-8");
                book1.setBIntroduction(introduction);
                String author = new String(request.getParameter("author").getBytes("ISO-8859-1"), "utf-8");
                book1.setBAuthor(author);
                String beizhu = new String(request.getParameter("beizhu").getBytes("ISO-8859-1"), "utf-8");
                book1.setBeizhu(beizhu);
                System.out.println(book1);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }


            if(!file.isEmpty()){
                String img = UUID.randomUUID().toString().replace("-","");
                int index = file.getOriginalFilename().lastIndexOf(".");
                String type = file.getOriginalFilename().substring(index);
                book1.setPhoto(img+"."+type);
                try {
                    file.transferTo(new File(image+book1.getPhoto()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            bookService.updateBook(book1);
        }
        return "redirect:search/add?yeshu=0";
    }

    @PostMapping("/add")
    public String addBook(@RequestParam("file")MultipartFile file,@Param("book") Book book1,HttpServletRequest request){
//        Integer accout = book1.getAccout();
//
//        if(accout < 0){
//            return "redirect:search/add?yeshu=0";
//        }

        try {
            String bName =new String(request.getParameter("bName").getBytes("ISO-8859-1"), "utf-8");
            book1.setBName(bName.trim());
            Book byName = bookService.findByName(book1);
            System.out.println("byName:"+byName);
            if(byName != null){
                return "redirect:search/add?yeshu=0";
            }


            String introduction = new String(request.getParameter("introduction").getBytes("ISO-8859-1"), "utf-8");
            book1.setBIntroduction(introduction.trim());
            String author = new String(request.getParameter("author").getBytes("ISO-8859-1"), "utf-8");
            book1.setBAuthor(author.trim());
            if(request.getParameter("beizhu") != ""){
                String beizhu = new String(request.getParameter("beizhu").getBytes("ISO-8859-1"), "utf-8");
                book1.setBeizhu(beizhu.trim());
            }
            book1.setRukushijian(new Date());
            book1.setYuliang(book1.getAccout());
            System.out.println(book1);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        if(!file.isEmpty()){
            String img = UUID.randomUUID().toString().replace("-","");
            int index = file.getOriginalFilename().lastIndexOf(".");
            String type = file.getOriginalFilename().substring(index);
            book1.setPhoto(img+"."+type);
            try {
                file.transferTo(new File(image+book1.getPhoto()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        bookService.insertBook(book1);
        return "redirect:search/add?yeshu=0";
    }
}
